import 'package:flutter/material.dart';
import 'package:flutter_base_library/index.dart';
import 'package:get/get.dart';
import 'package:badges/badges.dart' as badges;
import 'alarm_controller.dart';

class AlarmPage extends GetView<AlarmController> {
  const AlarmPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("报警事件"),
      ),
      body: Container(
        width: Get.width,
        color: const Color(0xFFEFEFF4),
        child: _bodyContent(context),
      ),
    );
  }

  Widget _bodyContent(BuildContext context) {
    return Obx(
      () => SmartRefresher(
        controller: controller.refreshController,
        enablePullDown: true,
        enablePullUp: false,
        header: Utils.customHeader,
        // footer: Utils.customFooter,
        onRefresh: controller.getEventNum,
        child: ListView.separated(
          padding: const EdgeInsets.fromLTRB(0, 10, 0, 0),
          itemCount: controller.dataList.length,
          separatorBuilder: (BuildContext context, int index) {
            return const Divider(height: 0.5);
          },
          itemBuilder: (BuildContext context, int index) {
            return GestureDetector(
              onTap: () {
                controller.listItemClicked(index);
              },
              behavior: HitTestBehavior.translucent,
              child: _listItemContent(index),
            );
          },
        ),
      ),
    );
  }

  Widget _listItemContent(int index) {
    AlarmTypeModel model = controller.dataList[index];
    return Container(
      width: Get.width,
      height: 68,
      color: Colors.white,
      child: Row(
        children: [
          const SizedBox(width: 16),
          badges.Badge(
            position: badges.BadgePosition.topEnd(top: -3, end: -5),
            showBadge: model.count != 0,
            ignorePointer: false,
            badgeStyle: badges.BadgeStyle(
              shape: badges.BadgeShape.square,
              badgeColor: Colors.red,
              padding: const EdgeInsets.fromLTRB(5, 3, 5, 3),
              borderRadius: BorderRadius.circular(10),
              // borderSide: BorderSide(color: Colors.white, width: 2),
              // borderGradient: badges.BadgeGradient.linear(
              //     colors: [Colors.red, Colors.black]),
              // badgeGradient: badges.BadgeGradient.linear(
              //   colors: [Colors.blue, Colors.yellow],
              //   begin: Alignment.topCenter,
              //   end: Alignment.bottomCenter,
              // ),
              elevation: 10,
            ),
            badgeContent: Text(
              model.count > 99 ? "99+" : model.count.toString(),
              style: const TextStyle(
                color: Colors.white,
                fontSize: 10,
                height: 1,
              ),
            ),
            child: Card(
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(5),
              ),
              clipBehavior: Clip.antiAlias,
              child: Image.asset(
                model.img,
                width: 50,
                height: 50,
              ),
            ),
          ),
          const SizedBox(width: 14),
          Expanded(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  model.title,
                  style: const TextStyle(
                    color: Color(0xff101010),
                    fontSize: 18,
                  ),
                ),
                Text(
                  model.count == 0 ? "没有待处理的报警消息" : "有待处理的报警消息",
                  style: const TextStyle(
                    color: Color(0xff9A9A9A),
                    fontSize: 14,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
